package com.pk.android.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView.OnItemClickListener;

import com.pk.android.R;
import com.pk.android.dummy.temp_source;
import com.pk.android.model.BaseFragment;
import com.pk.android.view.adapter.PlayersAdapter;

public class CreateTeamFragment extends BaseFragment {
 
	public static final String EXTRA_TITLE = "EXTRA_TITLE";
	
	PlayersAdapter playersAdapter;
	
	Button btnAllLeagues;
	ListView listViewPlayers;
	OnBuyPlayerClickListener mCallback;

	public interface OnBuyPlayerClickListener {
        public void onBuyPlayerClick();
    }
	
	public static final CreateTeamFragment newInstance(String title)
	{
		CreateTeamFragment f = new CreateTeamFragment();
		Bundle bdl = new Bundle(1);
	   bdl.putString(EXTRA_TITLE, title);
	   f.setArguments(bdl);

		return f;
	}
    
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnBuyPlayerClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnBuyPlayerClickListener");
        }
    }
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    	super.onCreateView(inflater, container, savedInstanceState);

    	view = inflater.inflate(R.layout.fragment_pk_create_team, container, false);
    	
    	((LinearLayout)view.findViewById(R.id.btn_buy_player)).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mCallback.onBuyPlayerClick();
			}
        });
    	
    	listViewPlayers = (ListView)view.findViewById(R.id.player_list);
		playersAdapter = new PlayersAdapter(getActivity(), temp_source.getMyPlayerList());
		listViewPlayers.setAdapter(playersAdapter);
		
		listViewPlayers.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
		listViewPlayers.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				listViewPlayers.setItemChecked(arg2, true);
				
			}
			
		});
		listViewPlayers.setMultiChoiceModeListener(new MultiChoiceModeListener(){

			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				// TODO Auto-generated method stub
				switch (item.getItemId()) {
				case R.id.action_buy:
					mode.finish();
					return true;
                case R.id.action_clear:
                    //# Calls getSelectedIds method from ListViewAdapter Class
                    //SparseBooleanArray selected = playersAdapter.getSelectedIds();
                    //# Captures all selected ids with a loop
                    //for (int i = (selected.size() - 1); i >= 0; i--) {
                      //  if (selected.valueAt(i)) {
                            //WorldPopulation selecteditem = playersAdapter.getItem(selected.keyAt(i));
                            //# Remove selected items following the ids
                            //playersAdapter.remove(selecteditem);
                        //}
                    //}
                    // Close CAB
                    mode.finish();
                    return true;
                default:
                    return false;
                }
			}

			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				// TODO Auto-generated method stub
				mode.getMenuInflater().inflate(R.menu.action_mode_buy_player, menu);
				return true;
			}

			@Override
			public void onDestroyActionMode(ActionMode arg0) {
				// TODO Auto-generated method stub
				//playersAdapter.removeSelection();
			}

			@Override
			public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void onItemCheckedStateChanged(ActionMode mode,
					int position, long id, boolean checked) {
				// TODO Auto-generated method stub
				// Calls toggleSelection method from ListViewAdapter Class
				final int checkCount = listViewPlayers.getCheckedItemCount();
				mode.setTitle(checkCount + " selected");
				//playersAdapter.toggleSelection(position);
				
			}});
		
        return view;
    }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
		super.onCreate(savedInstanceState);
		
		//registerForContextMenu(listViewPlayers);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
}
